/*
 * Copyright 2010 Christoph Widulle (christoph.widulle@googlemail.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the
 * License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS"
 * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language
 * governing permissions and limitations under the License.
 */



package com.android1.amarena2d.nodes.sprites;

import com.android1.amarena2d.nodes.Scene;
import com.android1.amarena2d.texture.TextureFrame;

public class BatchedSprite<T extends SpriteBatchNode> extends BaseSprite<T> {

    protected TextureFrame textureFrame;

    //To save method calls
    private int frameX;
    private int frameY;
    private int frameWidth;
    private int frameHeight;

    protected boolean flipX;
    protected boolean flipY;

    public BatchedSprite() {
        this(0, 0);
    }

    public BatchedSprite(float x, float y) {
        super(x, y);
    }

    BatchedSprite(TextureFrame textureFrame) {
        this(textureFrame, 0, 0);
    }

    BatchedSprite(TextureFrame textureFrame, float x, float y) {
        super(x, y);
        this.textureFrame = textureFrame;
    }


    @Override
    public void init() {
        if (!init) {
            updateSize();
            this.frameX = textureFrame.getX();
            this.frameY = textureFrame.getY();
            this.frameWidth = textureFrame.getWidth();
            this.frameHeight = textureFrame.getHeight();
            super.init();
        }
    }


    private void updateSize() {
        if (size.isZero()) {
            this.size.width = this.textureFrame.getWidth();
            this.size.height = this.textureFrame.getHeight();
        }
    }


    public void setTextureFrame(TextureFrame textureFrame) {
        this.textureFrame = textureFrame;
        this.frameX = textureFrame.getX();
        this.frameY = textureFrame.getY();
        this.frameWidth = textureFrame.getWidth();
        this.frameHeight = textureFrame.getHeight();

    }

    public TextureFrame getTextureFrame() {
        return textureFrame;
    }

    @Override
    public void render() {

        if (!init || !active || !visible || textureFrame == null)
            return;

        if (Scene.CULLING && !this.disableCulling && !this.isVisibleOnScreen())
            return;

        parent.spriteBatch.setColor(color);
        if (transformDelegate != null) {

            parent.spriteBatch.draw(parent.managedTexture.getTexture(), getX(), getY(),
                    transformDelegate.getTransformAnchorX(), transformDelegate.getTransformAnchorY(),
                    size.width, size.height, transformDelegate.getScaleX(), transformDelegate.getScaleY(), transformDelegate.getRotation(),
                    frameX, frameY, frameWidth, frameHeight, flipX, flipY);
        } else {

            parent.spriteBatch.draw(parent.managedTexture.getTexture(),
                    getX(), getY(),
                    size.width, size.height,
                    frameX, frameY, frameWidth, frameHeight, flipX, flipY);
        }
    }


    public boolean isFlipX() {
        return flipX;
    }

    public void setFlipX(boolean flipX) {
        this.flipX = flipX;
    }

    public boolean isFlipY() {
        return flipY;
    }

    public void setFlipY(boolean flipY) {
        this.flipY = flipY;
    }


    public void setSize(float width, float height) {
        this.size.width = width;
        this.size.height = height;

    }

    public void setWidth(float width) {
        this.size.width = width;
    }

    public void setHeight(float height) {
        this.size.height = height;
    }

}